<template>
  <div class="login-container">
    <!-- 登录的盒子 -->
    <div class="login-box">
      <img src="@/assets/logo.png" alt="" />
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item style="text-align: right">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from '@/api/login.js'
export default {
  name: 'login',
  data () {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 5,
            max: 10,
            message: '长度在 5 到 10 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '长度在 6 到 16 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        // 二次验证成功走这里
        if (valid) {
          // 1.获取用户数据
          // 2.发起请求登录
          // 3.登录成功提示
          // 登录失败提示
          // 请求成功走这里
          try {
            const res = await login(this.ruleForm)
            // 登录成功
            if (res.data.meta.status === 200) {
              // 存储token到本地
              localStorage.setItem('token', res.data.data.token)
              // 跳转到欢迎页
              this.$router.push('/welcome')
              // 登录成功提示
              this.$message('登录成功')
            } else { // 登录失败
              this.$message('登录失败,请稍后重试~~~')
            }
          } catch (error) {
            // 请求失败走这里
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang='less' scoped>
.login-container {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .login-box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 450px;
    height: 304px;
    background-color: #fff;
    transform: translate(-50%, -50%);
    img {
      position: absolute;
      left: 50%;
      width: 130px;
      height: 130px;
      background-color: #efefef;
      border: 10px solid #fff;
      border-radius: 50%;
      transform: translate(-50%, -50%);
    }
    .el-form {
      padding: 90px 20px;
    }
  }
}
</style>
